package com.bstek.pms.tools;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.MenuItem;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
import javafx.scene.image.Image;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.stage.StageStyle;

import java.io.IOException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.ResourceBundle;

/**
 * @Description  主控制类
 * @author hans
 * @CreateDate 2020/6/26
 */
public class Controller implements Initializable {

    private Map<String, Tab> openTabs = new HashMap<>();

    /*
     * @warn 注意：这里的属性名对应于fxml中的fx:id，在scene工具自动生成的xml中无fx，需要自己手工添加
     */
    @FXML
    private TabPane tabWorkspace;

    @FXML
    private MenuItem menuItemDBDocExcport;

    /**
     * 关于-->打开后的事件
     * @param actionEvent
     */
    @FXML
    private void onAboutAction(ActionEvent actionEvent){
        //函数名必需与fxml中的一致
        Alert alert = new Alert(Alert.AlertType.INFORMATION);
        alert.setTitle("提示");
        alert.setContentText("BSTEK 内部小工具\nDevelopment by hans");
        alert.show();
    }

    /**
     * 初始化时，先注册事件
     * @param location
     * @param resources
     */
    @Override
    public void initialize(URL location, ResourceBundle resources) {
        //数据库生成
        menuItemDBDocExcport.setOnAction(e -> openTab("view/db/doccreate/tabDBDocCreate.fxml"));
    }

    /**
     * 打开选项卡
     * @param fxmlFile
     */
    private void openTab(String fxmlFile) {
        //判断是否被打开过
        if (openTabs.containsKey(fxmlFile)) {
            tabWorkspace.getSelectionModel().select(openTabs.get(fxmlFile));
        } else {
            try {
                Tab myNewTab = FXMLLoader.load(this.getClass().getResource(fxmlFile));
                tabWorkspace.getTabs().add(myNewTab);
                openTabs.put(fxmlFile, myNewTab);
                //选中
                if(!myNewTab.isSelected()){
                    tabWorkspace.getSelectionModel().select(myNewTab);
                }
                myNewTab.setOnClosed(e -> openTabs.remove(fxmlFile));
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
        }
    }

    /**
     * @param actionEvent
     * @throws Exception
     */
    @FXML
    public void startLogin(ActionEvent actionEvent) throws Exception{
        Stage primaryStage=new Stage();
        primaryStage.setTitle("登录");
        primaryStage.getIcons().add(new Image("resources/img/icon.png"));
        Parent root = FXMLLoader.load(getClass().getResource("view/login/login.fxml"));
        primaryStage.setScene(new Scene(root));
        primaryStage.initStyle(StageStyle.DECORATED);
        primaryStage.setResizable(true);
        primaryStage.initModality(Modality.APPLICATION_MODAL);
        primaryStage.show();
    }
}
